package MOLE.Alipay;

import MOLE.*;

	public class Alipay
	{

		public String Mysign = "";
		public String Sign = "";
		public String ResponseTxt = "";
		public String Trade_Status = "";
		public String debugGoUrl = "";

		public String Fee = "";
		public String Trade_No = "";
		private CommInfo o;

		public Alipay(CommInfo oo)
		{
			//
			// TODO: �ڴ˴���ӹ��캯���߼�
			//
			o = oo;

		}



		//gogogo
		public final String CreatUrl()
//C# TO JAVA CONVERTER TODO TASK: There is no preprocessor in Java:
				///#region
		{
			/** 
			 created by sunzhizhi 2006.5.21,sunzhizhi@msn.com��
			*/
			int i;

			//�������飻
			String[] Oristr = {"service=" + o.service, "partner=" + o.partner, "subject=" + o.subject, "body=" + o.body, "out_trade_no=" + o.trade_no, "price=" + o.total_fee, "show_url=" + o.show_url, "payment_type=" + o.payment_type, "seller_email=" + o.seller_email, "notify_url=" + o.notify_url, "_input_charset=" + o._input_charset, "return_url=" + o.return_ur, "quantity=" + o.quantity};

			//��������
			Oristr = str.BubbleSort(Oristr);
			String [] Sortedstr = Oristr;
			 //�����md5ժҪ�ַ��� ��
			 String signStr = tangible.DotNetToJavaStringHelper.join("&",Oristr) + o.key.trim();
			 //����Md5ժҪ��
			String sign = EncryptDecrypt.MD5(signStr, o._input_charset);


			//����֧��Url��
			char[] delimiterChars = {'='};
			StringBuilder parameter = new StringBuilder();
			parameter.append(o.gateway);
			for (i = 0; i < Sortedstr.length; i++)
			{

				parameter.append(Sortedstr[i].split(java.util.regex.Pattern.quote(delimiterChars.toString()), -1)[0] + "=" + HttpUtility.UrlEncode(Sortedstr[i].split(java.util.regex.Pattern.quote(delimiterChars.toString()), -1)[1]) + "&");
			}

			parameter.append("sign=" + sign + "&sign_type=" + o.sign_type);
			String url = parameter.toString();


			//����toto Url��
			debugGoUrl = url.trim();
			Sign = sign;
			return (url.trim());

		}

//C# TO JAVA CONVERTER TODO TASK: There is no preprocessor in Java:
			///#endregion

		public final void GO()
		{
			   HttpContext.Current.Response.Redirect(CreatUrl());

		}











		//return onlly
		public final boolean CheckPass()
		{
//C# TO JAVA CONVERTER TODO TASK: There is no preprocessor in Java:
				///#region
			String mysign = getMySign().trim();
			String sign = System.Web.HttpContext.Current.Request["sign"].toString().trim();


			String alipayNotifyURL = "https://www.alipay.com/cooperate/gateway.do?";
			alipayNotifyURL = alipayNotifyURL + "service=notify_verify" + "&partner=" + o.partner + "&notify_id=" + HttpContext.Current.Request["notify_id"];
			String responseTxt = Get_Http(alipayNotifyURL, 120000);
			String tradeStatus = HttpContext.Current.Request["trade_status"];
			Sign = sign;
			Trade_Status = tradeStatus;



			if (mysign.equals(sign) && responseTxt.equals("true")) //��֤����������Ϣ��ǩ���Ƿ���ȷ
			{
				return true;
			}
			else
			{
				return false;
			}
//C# TO JAVA CONVERTER TODO TASK: There is no preprocessor in Java:
				///#endregion

		}



		//��ȡԶ�̷�����ATN���
		 private String Get_Http(String a_strUrl, int timeout)

		 {
//C# TO JAVA CONVERTER TODO TASK: There is no preprocessor in Java:
				///#region
			String strResult;
			try
			{

				HttpWebRequest myReq = (HttpWebRequest)HttpWebRequest.Create(a_strUrl);
				myReq.Timeout = timeout;
				HttpWebResponse HttpWResp = (HttpWebResponse)myReq.GetResponse();
				Stream myStream = HttpWResp.GetResponseStream();
				StreamReader sr = new StreamReader(myStream, Encoding.Default);
				StringBuilder strBuilder = new StringBuilder();
				while (-1 != sr.Peek())
				{
					strBuilder.append(sr.ReadLine());
				}

				strResult = strBuilder.toString();
			}
			catch (RuntimeException exp)
			{

				strResult = "����" + exp.getMessage();
			}
			ResponseTxt = strResult;
			return strResult;
//C# TO JAVA CONVERTER TODO TASK: There is no preprocessor in Java:
				///#endregion
		 }


		//get form alipay info check  result..
		//get FormParas+key(my check code set in alipay),then md5...
		public final String getMySign()
		{
//C# TO JAVA CONVERTER TODO TASK: There is no preprocessor in Java:
				 ///#region  ����md5 �ַ���
			int i;
			NameValueCollection coll;
			//Load Form variables into NameValueCollection variable.

			int formparaleng = HttpContext.Current.Request.Form.AllKeys.getLength();
			int questparaleng = HttpContext.Current.Request.QueryString.AllKeys.getLength();
			if (formparaleng > questparaleng) //notify tonzhi
			{
				coll = HttpContext.Current.Request.Form;
			}
			else
			{
				coll = HttpContext.Current.Request.QueryString;
			}

			// Get names of all forms into a string array.
			String[] requestarr = coll.AllKeys;



			//��������
			String[] Sortedstr = MOLE.str.BubbleSort(requestarr);



			//�����md5ժҪ�ַ��� ��
			String prestr = "";
			for (i = 0; i < Sortedstr.length; i++)
			{
				if (!HttpContext.Current.Request[Sortedstr[i]].equals("") && !Sortedstr[i].equals("sign") && !Sortedstr[i].equals("sign_type"))
				{
					if (i == Sortedstr.length - 1)
					{
						prestr = prestr + Sortedstr[i] + "=" + HttpContext.Current.Request[Sortedstr[i]];
					}
					else
					{
						prestr = prestr + Sortedstr[i] + "=" + HttpContext.Current.Request[Sortedstr[i]] + "&";
					}
				}

			}
			prestr = prestr + o.key;

			String mysign = EncryptDecrypt.MD5(prestr, "utf-8");
			Mysign = mysign;
			Fee = HttpContext.Current.Request["total_fee"].trim();
			Trade_No = HttpContext.Current.Request["out_trade_no"].trim();

			return mysign;

//C# TO JAVA CONVERTER TODO TASK: There is no preprocessor in Java:
				///#endregion
		}






	}